<?php


namespace app\api\controller\finance;


use app\api\controller\Api;
use app\common\order\model\OrderCollect;
use app\common\system\logic\UserDepartmentLogic;
use app\common\system\logic\UserLogic;
use util\Format;

class Unincome extends Api
{
    public function index()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $where = [];
        if ($user['user_type'] == 2) {
            $userLogic = new UserLogic();
            $staffIds = $userLogic->getDptUserIds($user['id']);
            $where[] = ['user_id', 'in', $staffIds];
        } else {
            $where[] = ['user_id', '=', $user['id']];
        }

        if (!empty($post['department_id'])) {
            $dptLogic = new UserDepartmentLogic();
            $staffIds = $dptLogic->getDptUserIds($post['department_id']);
            $where[] = ['user_id', 'in', $staffIds];
        }
        if (!empty($post['user_id'])) $where[] = ['user_id', 'in', $post['user_id']];
        if (!empty($post['store_id'])) $where[] = ['store_id', 'in', $post['store_id']];

        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $where[] = ['sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $where[] = ['event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $where[] = ['order_no', 'like', "%{$post['keyword']}%"];

        if (!empty($post['status'])) $where[] = ['status', '=', $post['status']];

        list($cfg) = Format::pagination($post);
        $model = new \app\common\order\model\Order();
        $model = $model->where($where);
        if (!empty($post['source_id'])) {
            $hasWhere = [];
            $hasWhere[] = ['source_id', 'in', $post['source_id']];
            $model = $model->whereIn('member_id', function ($query) use ($hasWhere) {
                $query->table('tk_member')->where($hasWhere)->where('delete_time', 0)->field('id');
            });
        }

        if (empty($post['is_export'])) {
            // 查询
            $pg = $model->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['source', 'user', 'vip', 'bom', 'income_totals']);
            }

            // 返回接口数据
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);

            return success($result, '获取数据成功');
        } else {

            // 审核状态	订单类型 订单性质 所属公司 渠道来源	合同模式 合同编号	签单销售	签约日期	预产期	姓名	电话	酒店名称	宴会厅	创建时间	
            $header = [
                'source' => '渠道来源',
            ];

            $rows = $model->order('id desc')->select();
            $expData = [];
            foreach ($rows as $row) {
                $attrs = ['user', 'store'];
                $row->append($attrs);
                $data = [];

                $expData[] = $data;
            }

            // print_r($expData);
            $fileName = '【订单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }

    // 我的应收单
    public function mine()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $where = [];
        $where[] = ['user_id', '=', $user['id']];
        if (!empty($post['user_id'])) $where[] = ['user_id', 'in', $post['user_id']];
        if (!empty($post['store_id'])) $where[] = ['store_id', 'in', $post['store_id']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $where[] = ['sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $where[] = ['event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $where[] = ['order_no', 'like', "%{$post['keyword']}%"];

        if (!empty($post['status'])) $where[] = ['status', '=', $post['status']];

        list($cfg) = Format::pagination($post);
        $model = new \app\common\order\model\Order();
        $model = $model->where($where);
        if (!empty($post['source_id'])) {
            $hasWhere = [];
            $hasWhere[] = ['source_id', 'in', $post['source_id']];
            $model = $model->whereIn('member_id', function ($query) use ($hasWhere) {
                $query->table('tk_member')->where($hasWhere)->where('delete_time', 0)->field('id');
            });
        }

        if (empty($post['is_export'])) {
            // 查询
            $pg = $model->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['source', 'user', 'vip', 'bom', 'income_totals']);
            }

            // 返回接口数据
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);

            return success($result, '获取数据成功');
        } else {

            // 审核状态	订单类型 订单性质 所属公司 渠道来源	合同模式 合同编号	签单销售	签约日期	预产期	姓名	电话	酒店名称	宴会厅	创建时间	
            $header = [
                'source' => '渠道来源',
            ];

            $rows = $model->order('id desc')->select();
            $expData = [];
            foreach ($rows as $row) {
                $attrs = ['user', 'store'];
                $row->append($attrs);
                $data = [];

                $expData[] = $data;
            }

            // print_r($expData);
            $fileName = '【订单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }
}
